﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//暴力搜索字符串
namespace violentSearcher
{
    class ViolentSearcher
    {
        private string pattern;
        private char wildcard = '*';

        public ViolentSearcher(string inputPattern)
        {
            pattern = inputPattern;
        }

        private List<int> SearchInString(string text)
        {
            List<int> positions = new List<int>();
            int textLength = text.Length;
            int patternLength = pattern.Length;

            for (int i = 0; i <= textLength - patternLength; i++)
            {
                bool match = true;
                for (int j = 0; j < patternLength; j++)
                {
                    if (pattern[j] != wildcard && pattern[j] != text[i + j])
                    {
                        match = false;
                        break;
                    }
                }
                if (match)
                {
                    positions.Add(i); // 添加起始位置到列表
                }
            }
            return positions;
        }

        public void SearchInStream(string filePath)
        {
            byte[] fileBytes;
            string text = "";
            try
            {
                fileBytes = File.ReadAllBytes(filePath);
                text = Encoding.UTF8.GetString(fileBytes);
            }
            catch (Exception e)
            {
                Console.WriteLine($"Error reading file: {e.Message}");
                return;
            }
           
            List<int> startPositions = SearchInString(text);
            if (startPositions.Count > 0)
            {
                Console.WriteLine("暴力搜索结果如下：");
                Console.WriteLine("模式串起始位置为：");
                foreach (int position in startPositions)
                {
                    Console.WriteLine(position);
                }
            }
            else
            {
                Console.WriteLine("模式串未找到。");
            }
        }
    }
}
